package com.tenebrous.jestic.controller.framwork.meber.organ;

import com.github.pagehelper.PageInfo;
import com.tenebrous.jestic.compents.annotations.Weblog;
import com.tenebrous.jestic.compents.annotations.WeblogEnum;
import com.tenebrous.jestic.controller.SupperController;
import com.tenebrous.jestic.entity.framwork.meber.organ.*;
import com.tenebrous.jestic.service.framwork.meber.organ.*;
import com.rain.ops.sdk.dto.ApiResult;
import com.rain.ops.sdk.utils.UUIDUtils;
import com.tenebrous.jestic.entity.framwork.meber.organ.*;
import com.tenebrous.jestic.service.framwork.meber.organ.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

@Validated
@RestController
@RequestMapping("/organ/user/ext")
public class UserExtController extends SupperController {

    @Autowired
    private OrgUserWorkService userWorkService;

    @Autowired
    private OrgUserTrainService userTrainService;

    @Autowired
    private OrgUserAwardService userAwardService;

    @Autowired
    private OrgUserCertieService userCertiService;

    @Autowired
    private OrgUserFamilyService userFamilyService;

    @Autowired
    private OrgUserEducateService userEducateService;

    /**
     * @param id
     * @return
     */
    @PostMapping("/educate/get")
    public ApiResult getEducate(@NotBlank String id) {
        OrgUserEducate ouc = userEducateService.selectByPrimaryKey(id);
        if (ouc != null) {
            return new ApiResult(200, "查询成功。", ouc);
        }
        return new ApiResult(999, "查询失败，未找到指定记录。");
    }

    /**
     * 获取教育经历
     *
     * @param uid 用户id
     * @return
     */
    @PostMapping("/educate/query")
    public ApiResult queryEducateList(@NotBlank String uid) {
        List<OrgUserEducate> oucList = userEducateService.getEducateList(uid);
        return new ApiResult(200, "查询成功。", oucList);
    }

    /**
     * 分页查询教育经历
     *
     * @param uid
     * @return
     */
    @PostMapping("/educate/paging")
    public ApiResult pagingEducateList(@NotBlank String uid) {
        PageInfo pageInfo = userEducateService.queryPagingList(super.getPageIndex(), super.getPageSize(), uid);
        return new ApiResult(200, "查询成功。", pageInfo);
    }

    /**
     * 保存或更新教育经历
     *
     * @param item
     * @return
     */
    @PostMapping("/educate/supdate")
    @Weblog(description = "保存或更新教育经历", method = WeblogEnum.UPDATE)
    public ApiResult supdateEducate(@NotNull OrgUserEducate item) {
        Date dt = new Date();
        OrgUser ouser = super.getCurrentUser();
        if (StringUtils.isNotBlank(item.getId())) {
            item.setUpdateDate(dt);
            item.setUpdateUserid(ouser.getId());
            this.userEducateService.updateSelective(item);
        } else {
            item.setCreateDate(dt);
            item.setId(UUIDUtils.randomUUID());
            item.setCreateUserid(ouser.getId());
            this.userEducateService.saveSelective(item);
        }
        return new ApiResult(200, "更新成功。", item);
    }

    /**
     * 更新教育经历
     *
     * @param educateStr
     * @param uid
     * @return
     */
    @PostMapping("/update-educate")
    @Weblog(description = "更新教育经历", method = WeblogEnum.UPDATE)
    public ApiResult saveOrUpdateEducate(String educateStr, String uid) {
        OrgUser user = super.getCurrentUser();
        return userEducateService.saveOrUpdateEducate(educateStr, uid, user);
    }

    /**
     * 删除教育经历
     *
     * @param ids
     * @return
     */
    @PostMapping("/educate/delete")
    @Weblog(description = "删除教育经历", method = WeblogEnum.DELETE)
    public ApiResult deleteEducate(@NotEmpty String[] ids) {
        return new ApiResult(200, "删除成功。", userEducateService.deleteByIds(ids));
    }

    /**
     * 查找工作履历
     *
     * @param id
     * @return
     */
    @PostMapping("/work/get")
    public ApiResult getWork(@NotBlank String id) {
        OrgUserWork ouw = userWorkService.selectByPrimaryKey(id);
        if (ouw != null) {
            return new ApiResult(200, "查询成功。", ouw);
        }
        return new ApiResult(999, "查询失败，未找到指定记录。");
    }

    /**
     * 获取工作经历
     *
     * @param uid 用户id
     * @return
     */
    @PostMapping("/work/query")
    public ApiResult queryWorkList(@NotBlank String uid) {
        List<OrgUserWork> ouwList = userWorkService.getWorkList(uid);
        return new ApiResult(200, "查询成功。", ouwList);
    }

    /**
     * 分页查询工作经历
     *
     * @param uid
     * @return
     */
    @PostMapping("/work/paging")
    public ApiResult pagingWorkList(@NotBlank String uid) {
        PageInfo pageInfo = userWorkService.queryPagingList(super.getPageIndex(), super.getPageSize(), uid);
        return new ApiResult(200, "查询成功。", pageInfo);
    }

    /**
     * 保存或更新工作履历
     *
     * @param item
     * @return
     */
    @PostMapping("/work/supdate")
    @Weblog(description = "保存或更新工作履历", method = WeblogEnum.UPDATE)
    public ApiResult supdateWork(@NotNull OrgUserWork item) {
        Date dt = new Date();
        OrgUser ouser = super.getCurrentUser();
        if (StringUtils.isNotBlank(item.getId())) {
            item.setUpdateDate(dt);
            item.setUpdateUserid(ouser.getId());
            this.userWorkService.updateSelective(item);
        } else {
            item.setCreateDate(dt);
            item.setId(UUIDUtils.randomUUID());
            item.setCreateUserid(ouser.getId());
            this.userWorkService.saveSelective(item);
        }
        return new ApiResult(200, "更新成功。", item);
    }

    /**
     * 更新工作经历
     *
     * @param workStr
     * @param uid
     * @return
     */
    @PostMapping("/update-work")
    @Weblog(description = "更新工作经历", method = WeblogEnum.UPDATE)
    public ApiResult saveOrUpdateWork(String workStr, String uid) {
        OrgUser user = super.getCurrentUser();
        return userWorkService.saveOrUpdateWork(workStr, uid, user);
    }

    /**
     * 删除工作经历
     *
     * @param ids
     * @return
     */
    @PostMapping("/work/delete")
    @Weblog(description = "删除工作经历", method = WeblogEnum.DELETE)
    public ApiResult deleteWork(@NotEmpty String[] ids) {
        return new ApiResult(200, "删除成功。", userWorkService.deleteByIds(ids));
    }

    /**
     * @param id
     * @return
     */
    @PostMapping("/train/get")
    public ApiResult getTrain(@NotBlank String id) {
        OrgUserTrain out = userTrainService.selectByPrimaryKey(id);
        if (out != null) {
            return new ApiResult(200, "查询成功。", out);
        }
        return new ApiResult(999, "查询失败，未找到指定记录。");
    }

    /**
     * 获取培训经历
     *
     * @param uid 用户id
     * @return
     */
    @PostMapping("/train/query")
    public ApiResult queryTrainList(@NotBlank String uid) {
        List<OrgUserTrain> outList = userTrainService.getTrainList(uid);
        return new ApiResult(200, "查询成功。", outList);
    }

    /**
     * 分页查询培训经历
     *
     * @param uid
     * @return
     */
    @PostMapping("/train/paging")
    public ApiResult pagingTrainList(@NotBlank String uid) {
        PageInfo pageInfo = userTrainService.queryPagingList(super.getPageIndex(), super.getPageSize(), uid);
        return new ApiResult(200, "查询成功。", pageInfo);
    }

    /**
     * 保存或更新培训经历
     *
     * @param item
     * @return
     */
    @PostMapping("/train/supdate")
    @Weblog(description = "保存或更新培训经历", method = WeblogEnum.UPDATE)
    public ApiResult supdateWork(@NotNull OrgUserTrain item) {
        Date dt = new Date();
        OrgUser ouser = super.getCurrentUser();
        if (StringUtils.isNotBlank(item.getId())) {
            item.setUpdateDate(dt);
            item.setUpdateUserid(ouser.getId());
            this.userTrainService.updateSelective(item);
        } else {
            item.setCreateDate(dt);
            item.setId(UUIDUtils.randomUUID());
            item.setCreateUserid(ouser.getId());
            this.userTrainService.saveSelective(item);
        }
        return new ApiResult(200, "更新成功。", item);
    }

    /**
     * 更新培训经历
     *
     * @param trainStr
     * @param uid
     * @return
     */
    @PostMapping("/update-train")
    @Weblog(description = "更新培训经历", method = WeblogEnum.UPDATE)
    public ApiResult saveOrUpdateTrain(String trainStr, String uid) {
        OrgUser user = super.getCurrentUser();
        return userTrainService.saveOrUpdateTrain(trainStr, uid, user);
    }

    /**
     * 删除培训经历
     *
     * @param ids
     * @return
     */
    @PostMapping("/train/delete")
    @Weblog(description = "删除培训经历", method = WeblogEnum.DELETE)
    public ApiResult deleteTrain(@NotEmpty String[] ids) {
        return new ApiResult(200, "删除成功。", userTrainService.deleteByIds(ids));
    }

    /**
     * @param id
     * @return
     */
    @PostMapping("/get-award")
    public ApiResult getAward(@NotBlank String id) {
        OrgUserAward oua = userAwardService.selectByPrimaryKey(id);
        if (oua != null) {
            return new ApiResult(200, "查询成功。", oua);
        }
        return new ApiResult(999, "查询失败，未找到指定记录。");
    }

    /**
     * 获取奖惩情况
     *
     * @param uid 用户id
     * @return
     */
    @PostMapping("/get-award-list")
    public ApiResult getAwardList(@NotBlank String uid) {
        List<OrgUserAward> outList = userAwardService.getAwardList(uid);
        return new ApiResult(200, "查询成功。", outList);
    }

    /**
     * 获取奖惩情况分页
     * chengb
     *
     * @param uid 用户id
     * @return
     */
    @PostMapping("/querypage-award")
    public ApiResult queryPageAward(@NotBlank String uid, String type) {
        PageInfo<OrgUserAward> pageInfo = userAwardService.queryPagingList(super.getPageIndex(), super.getPageSize(), uid, type);
        return new ApiResult(200, "查询成功。", pageInfo);
    }

    /**
     * 更新奖惩情况
     * chengb
     *
     * @param entity
     * @return
     */
    @PostMapping("/update-award1")
    @Weblog(description = "更新奖惩情况", method = WeblogEnum.UPDATE)
    public ApiResult saveOrUpdateAward1(OrgUserAward entity) {
        OrgUser user = super.getCurrentUser();
        return userAwardService.saveOrUpdateAward1(entity, user);
    }

    /**
     * 更新奖惩情况
     *
     * @param awardStr
     * @param uid
     * @return
     */
    @PostMapping("/update-award")
    @Weblog(description = "更新奖惩情况", method = WeblogEnum.UPDATE)
    public ApiResult saveOrUpdateAward(String awardStr, String uid) {
        OrgUser user = super.getCurrentUser();
        return userAwardService.saveOrUpdateAward(awardStr, uid, user);
    }

    /**
     * 删除奖惩情况
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete-award")
    @Weblog(description = "删除奖惩情况", method = WeblogEnum.DELETE)
    public ApiResult deleteAward(@NotEmpty String[] ids) {
        return new ApiResult(200, "删除成功。", userAwardService.deleteByIds(ids));
    }

    /**
     * @param id
     * @return
     */
    @PostMapping("/family/get")
    public ApiResult getFamily(@NotBlank String id) {
        OrgUserFamily ouf = userFamilyService.selectByPrimaryKey(id);
        if (ouf != null) {
            return new ApiResult(200, "查询成功。", ouf);
        }
        return new ApiResult(999, "查询失败，未找到指定记录。");
    }

    /**
     * 获取家庭成员情况
     *
     * @param uid 用户id
     * @return
     */
    @PostMapping("/family/query")
    public ApiResult queryFamilyList(@NotBlank String uid) {
        List<OrgUserFamily> outList = userFamilyService.queryListByUserid(uid);
        return new ApiResult(200, "查询成功。", outList);
    }

    /**
     * 分页查询家庭成员情况
     *
     * @param uid
     * @return
     */
    @PostMapping("/family/paging")
    public ApiResult pagingFamilyList(@NotBlank String uid) {
        PageInfo pageInfo = userFamilyService.queryPagingList(super.getPageIndex(), super.getPageSize(), uid);
        return new ApiResult(200, "查询成功。", pageInfo);
    }

    /**
     * 保存或更新家庭成员情况
     *
     * @param item
     * @return
     */
    @PostMapping("/family/supdate")
    @Weblog(description = "保存或更新家庭成员情况", method = WeblogEnum.UPDATE)
    public ApiResult supdateFamily(@NotNull OrgUserFamily item) {
        Date dt = new Date();
        OrgUser ouser = super.getCurrentUser();
        if (StringUtils.isNotBlank(item.getId())) {
            item.setUpdateDate(dt);
            item.setUpdateUserid(ouser.getId());
            this.userFamilyService.updateSelective(item);
        } else {
            item.setCreateDate(dt);
            item.setId(UUIDUtils.randomUUID());
            item.setCreateUserid(ouser.getId());
            this.userFamilyService.saveSelective(item);
        }
        return new ApiResult(200, "更新成功。", item);
    }

    /**
     * 更新家庭成员情况
     *
     * @param familyStr
     * @param uid
     * @return
     */
    @PostMapping("/update-family")
    @Weblog(description = "更新家庭成员情况", method = WeblogEnum.UPDATE)
    public ApiResult saveOrUpdateFamily(String familyStr, String uid) {
        OrgUser user = super.getCurrentUser();
        return userFamilyService.saveOrUpdateFamily(familyStr, uid, user);
    }

    /**
     * 删除家庭成员情况
     *
     * @param ids
     * @return
     */
    @PostMapping("/family/delete")
    @Weblog(description = "删除家庭成员情况", method = WeblogEnum.DELETE)
    public ApiResult deleteFamily(@NotEmpty String[] ids) {
        return new ApiResult(200, "删除成功。", userFamilyService.deleteByIds(ids));
    }

    /**
     * 查询证书
     *
     * @param id
     * @return
     */
    @PostMapping("/certie/get")
    public ApiResult getCertie(@NotBlank String id) {
        OrgUserCerti ouf = userCertiService.selectByPrimaryKey(id);
        if (ouf != null) {
            return new ApiResult(200, "查询成功。", ouf);
        }
        return new ApiResult(999, "查询失败，未找到指定记录。");
    }

    /**
     * 查询证书
     *
     * @param uid 用户id
     * @return
     */
    @PostMapping("/certie/query")
    public ApiResult queryCertieList(@NotBlank String uid) {
        List<OrgUserCerti> outList = userCertiService.queryCertieList(uid);
        return new ApiResult(200, "查询成功。", outList);
    }

    /**
     * 分页查询证书
     *
     * @param uid
     * @return
     */
    @PostMapping("/certie/paging")
    public ApiResult pagingCertieList(@NotBlank String uid) {
        PageInfo pageInfo = userCertiService.queryPagingList(super.getPageIndex(), super.getPageSize(), uid);
        return new ApiResult(200, "查询成功。", pageInfo);
    }

    /**
     * 保存或更新证书
     *
     * @param item
     * @return
     */
    @PostMapping("/certie/supdate")
    @Weblog(description = "保存或更新证书", method = WeblogEnum.UPDATE)
    public ApiResult supdateCertie(@NotNull OrgUserCerti item) {
        Date dt = new Date();
        OrgUser ouser = super.getCurrentUser();
        if (StringUtils.isNotBlank(item.getId())) {
            item.setUpdateDate(dt);
            item.setUpdateUserid(ouser.getId());
            this.userCertiService.updateSelective(item);
        } else {
            item.setCreateDate(dt);
            item.setId(UUIDUtils.randomUUID());
            item.setCreateUserid(ouser.getId());
            this.userCertiService.saveSelective(item);
        }
        return new ApiResult(200, "更新成功。", item);
    }

    /**
     * 删除证书
     *
     * @param ids
     * @return
     */
    @PostMapping("/certie/delete")
    @Weblog(description = "删除证书", method = WeblogEnum.DELETE)
    public ApiResult deleteCertie(@NotEmpty String[] ids) {
        return new ApiResult(200, "删除成功。", userCertiService.deleteByIds(ids));
    }
}
